    //
//  HomeViewController.m
//  OrderOnline
//
//  Created by YAPPA_3 on 11/28/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "HomeViewController.h"
#import "ThumbViewController.h"
#import "OrderOnlineViewController.h"
#import "DataManager.h"


#define MARGIN_WIDTH_PORT_IPAD 9.6
#define MARGIN_WIDTH_LAND_IPAD 20
#define MARGIN_HEIGHT_IPAD_LAND 3
#define MARGIN_HEIGHT_IPAD_PORT 5
#define NUM_OF_COLUMN_LAND_IPAD 5
#define NUM_OF_COLUMN_PORT_IPAD 4
#define THUMB_WIDTH_IPAD		180	
#define THUMB_HEIGHT_IPAD		220

@implementation HomeViewController
@synthesize m_scrollView;
@synthesize m_listThumbAll;
@synthesize m_barButtonSlide;
@synthesize m_naviBar;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        m_listThumbAll = [[NSMutableArray alloc] init];	
		m_listThumbFood = [[NSMutableArray alloc] init];
		m_listThumbDrink = [[NSMutableArray alloc] init];
		m_isSlideshow = NO;
		m_isSlideshow = [OrderOnlineViewController Shared].m_isSlideShow;
		m_typeMenu = [OrderOnlineViewController Shared].m_typeMenu;
				
	}
    return self;
}

- (void) addThumbToScrollView
{  
	NSMutableArray* m_arrayList;
	if (m_typeMenu == TYPE_MENU_ALL) {
		m_arrayList = m_listThumbAll;
	}
	if (m_typeMenu == TYPE_MENU_FOOD) {
		m_arrayList = m_listThumbFood;
	}
	if (m_typeMenu == TYPE_MENU_DRINK) {
		m_arrayList = m_listThumbDrink;
	}	
   	
	for(UIView* subView in [m_scrollView subviews]){
		if ([subView isKindOfClass:[UIView class]]) {
			[subView removeFromSuperview];
		}
	}
	
	for (int i = 0; i < [m_arrayList count]; i++) {
		ThumbViewController* thumb = [m_arrayList objectAtIndex:i];
		[m_scrollView addSubview:thumb.view];
	}
}

- (void) loadListThumb
{
	SAFE_RELEASE(m_listThumbAll)
	m_listThumbAll = [[NSMutableArray alloc] init];
	NSMutableArray* listFood = [DataManager Shared].m_XmlParser.m_menuFood.m_listFood;
	for (int i = 0; i < [listFood count]; i++) {
		Food* food = [listFood objectAtIndex:i];
		ThumbViewController* thumbView = [[ThumbViewController alloc] initWithNibName:@"ThumbViewController" bundle:nil];
		NSString* imagePath = [[NSBundle mainBundle] pathForResource: food.m_linkImage ofType:@"jpg"];
		UIImage* image = [UIImage imageWithContentsOfFile:imagePath];
		[thumbView view];	
		thumbView.m_food = food;
		thumbView.m_pageNo = i;
		[[thumbView m_thumbImage]  initVisible]; 
		[thumbView.m_thumbImage setImageToButton:image]; 
		[m_listThumbAll addObject:thumbView];
		[thumbView release];

	}				
}
- (CGPoint) getThumbFrameAt:(int)index withOrientation: (UIInterfaceOrientation)orie
{
	CGFloat x, y;
    BOOL isLand = isDeviceLandScape(orie);
	int column;
	int row;
	if(isLand){
		column = index%NUM_OF_COLUMN_LAND_IPAD;
		row = index/NUM_OF_COLUMN_LAND_IPAD;
		x = MARGIN_WIDTH_LAND_IPAD + column*THUMB_WIDTH_IPAD + MARGIN_WIDTH_LAND_IPAD*column;
		y = MARGIN_HEIGHT_IPAD_LAND + row*THUMB_HEIGHT_IPAD + MARGIN_HEIGHT_IPAD_LAND*row;
	}
	else {
		column = index%NUM_OF_COLUMN_PORT_IPAD;
		row = index/NUM_OF_COLUMN_PORT_IPAD;
		x = MARGIN_WIDTH_PORT_IPAD + column*THUMB_WIDTH_IPAD + MARGIN_WIDTH_PORT_IPAD*column;
		y = MARGIN_HEIGHT_IPAD_PORT + row*THUMB_HEIGHT_IPAD + MARGIN_HEIGHT_IPAD_PORT*row;
	}		
	return CGPointMake(x, y);	
}

- (void) correctThumbFrame
{
    UIInterfaceOrientation orie = [OrderOnlineViewController Shared].m_DeviceOrie;  	    
    BOOL isLand = isDeviceLandScape(orie);
	NSMutableArray* m_arrayList;
	if (m_typeMenu == TYPE_MENU_ALL) {
		m_arrayList = m_listThumbAll;
	}
	if (m_typeMenu == TYPE_MENU_FOOD) {
		m_arrayList = m_listThumbFood;
	}
	if (m_typeMenu == TYPE_MENU_DRINK) {
		m_arrayList = m_listThumbDrink;
	}
    int count = [m_arrayList count];
    for (int i = 0; i < count; i++) {
        ThumbViewController* thumb = [m_arrayList objectAtIndex:i];
        CGPoint thumb_coor = [self getThumbFrameAt:i withOrientation:orie];       
		if (isLand) {
			[thumb.view setFrame:CGRectMake(thumb_coor.x, thumb_coor.y, THUMB_WIDTH_IPAD, THUMB_HEIGHT_IPAD)];    
		}
		else
		{
			[thumb.view setFrame:CGRectMake(thumb_coor.x, thumb_coor.y, THUMB_WIDTH_IPAD, THUMB_HEIGHT_IPAD)];      
				
		}	                 
    } 
    
    int numRow;	
	if (isLand) {
		if(([m_arrayList count]%NUM_OF_COLUMN_LAND_IPAD) != 0)
		{
			numRow = [m_arrayList count]/NUM_OF_COLUMN_LAND_IPAD + 1;			
		}
		else
		{
			numRow = [m_arrayList count]/NUM_OF_COLUMN_LAND_IPAD;
		}
		[m_scrollView setContentSize:CGSizeMake(1024, numRow*THUMB_HEIGHT_IPAD + (numRow + 1)*MARGIN_HEIGHT_IPAD_LAND)];
	}
	else{			
		if(([m_arrayList count]%NUM_OF_COLUMN_PORT_IPAD) != 0)
		{
			numRow = [m_arrayList count]/NUM_OF_COLUMN_PORT_IPAD + 1;			
		}
		else
		{
			numRow = [m_arrayList count]/NUM_OF_COLUMN_PORT_IPAD;
		}	
		
		[m_scrollView setContentSize:CGSizeMake(768, THUMB_HEIGHT_IPAD*numRow + (numRow + 1)*MARGIN_HEIGHT_IPAD_PORT)];
	}	
	
}


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	if (m_typeMenu == TYPE_MENU_ALL) {
		[m_naviBar.topItem setTitle:@"Menu"];
	}
	if (m_typeMenu == TYPE_MENU_FOOD) {
		[m_naviBar.topItem setTitle:@"Menu Food"];
	}
	if (m_typeMenu == TYPE_MENU_DRINK) {
		[m_naviBar.topItem setTitle:@"Menu Drinks"];
	}
	
	[self loadListThumb];
	[self loadListThumbByType:m_typeMenu];
	[self correctThumbFrame];
	[self addThumbToScrollView];
	 
}



- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Overriden to allow any orientation.
    return YES;
}


- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void) loadListThumbByType: (int) typeMenu
{
	switch (typeMenu) {			
			
		case TYPE_MENU_FOOD:
			SAFE_RELEASE(m_listThumbFood)
			m_listThumbFood = [[NSMutableArray alloc] init];
			for (int i = 0; i < [m_listThumbAll count]; i++) {
				ThumbViewController* thumbView = [m_listThumbAll objectAtIndex:i];
				if ([thumbView.m_food.m_type isEqualToString:@"food"]) {
					[m_listThumbFood addObject:thumbView];
				}
			}
			break;
			
		case TYPE_MENU_DRINK:
			SAFE_RELEASE(m_listThumbDrink)
			m_listThumbDrink = [[NSMutableArray alloc] init];
			for (int i = 0; i < [m_listThumbAll count]; i++) {
				ThumbViewController* thumbView = [m_listThumbAll objectAtIndex:i];
				if ([thumbView.m_food.m_type isEqualToString:@"drink"]) {
					[m_listThumbDrink addObject:thumbView];
				}
			}
			
			break;			
		default:
			break;
	}
}
- (void) getListFood: (id) sender
{
	m_typeMenu = TYPE_MENU_FOOD;
	[OrderOnlineViewController Shared].m_typeMenu = m_typeMenu;
	[m_naviBar.topItem setTitle:@"Menu Food"];
	[self loadListThumbByType:m_typeMenu];
	[self correctThumbFrame];
	[self addThumbToScrollView];
}

- (void) getListDrink: (id) sender
{
	m_typeMenu = TYPE_MENU_DRINK;
	[OrderOnlineViewController Shared].m_typeMenu = m_typeMenu;
	[m_naviBar.topItem setTitle:@"Menu Drinks"];
	[self loadListThumbByType:m_typeMenu];
	[self correctThumbFrame];
	[self addThumbToScrollView];
}

- (void) getListAll: (id) sender
{
	m_typeMenu = TYPE_MENU_ALL;
	[OrderOnlineViewController Shared].m_typeMenu = m_typeMenu;
	[m_naviBar.topItem setTitle:@"Menu"];
	[self loadListThumbByType:m_typeMenu];
	[self correctThumbFrame];
	[self addThumbToScrollView];
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void) autoScrollView: (id) sender
{
	[OrderOnlineViewController Shared].m_typeMenu = m_typeMenu;
	m_isSlideshow = !m_isSlideshow;
	[OrderOnlineViewController Shared].m_isSlideShow = m_isSlideshow;
	if (m_isSlideshow) {
		[m_barButtonSlide setTitle:@"Stop Slideshow"];
	}
	else {
		[m_barButtonSlide setTitle:@"Start Slideshow"];
	}

	[[OrderOnlineViewController Shared] changeReadView:NO];
	[[OrderOnlineViewController Shared] setSlideShow: m_isSlideshow];
}

- (void)dealloc {
	[m_scrollView release];
	[m_listThumbAll release];
	SAFE_RELEASE( m_listThumbFood);
	SAFE_RELEASE( m_listThumbDrink);
	[m_barButtonSlide release];
	[m_naviBar release];
    [super dealloc];
}

- (void) changeToOrder: (id) sender
{
	[[OrderOnlineViewController Shared]  changeOrderView:YES];
}

@end
